﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyLibary.DAL;

namespace MyLibary.BIZ
{
    public class BIZ_ChiTietNhap
    {
        #region Fields
        public string MaHDNhap { get; set; }
        public string MaSanPham { get; set; }
        public decimal GiaNhap{get;set;}
        public int SoLuong { get; set; }
        public BIZ_SanPham SanPham { get { return DAL_SanPham.GetById(this.MaSanPham); } }
        public BIZ_HoaDonNhap HDNhap { get { return DAL_HoaDonNhap.GetByID(this.MaHDNhap); } }
        #endregion
        #region Contructor
        public BIZ_ChiTietNhap() { }
        public BIZ_ChiTietNhap(string MaHDNhap, String MaSanPham, decimal GiaNhap, int SoLuong)
        {
            this.MaHDNhap = MaHDNhap;
            this.MaSanPham = MaSanPham;
            this.GiaNhap = GiaNhap;
            this.SoLuong = SoLuong;
        }
        #endregion
        #region Metholds
        public static List<BIZ_ChiTietNhap> getAll(string MaHDNhap)
        {
            return DAL_ChiTietNhap.getAll(MaHDNhap);
        }
        public static List<BIZ_ChiTietNhap> getBySanPham(string MaSanPham, DateTime date)
        {
            return DAL_ChiTietNhap.getBySanPham(MaSanPham, date);
        }
        public static decimal getTongTien(string MaHDNhap)
        {
            return DAL_ChiTietNhap.getTongTien( MaHDNhap);
        }
        public static int GetSoLuong(string MaSanPham, DateTime Ngay)
        {
            return DAL_ChiTietNhap.getSoLuong(MaSanPham, Ngay);
        }
        public static decimal getThanhTien(string MaSanPham, DateTime Ngay)
        {
            return DAL_ChiTietNhap.getThanhTien(MaSanPham, Ngay);
        }
        public bool Add()
        {
            bool success= DAL_ChiTietNhap.Insert(this);
            if (success)
            {
                BIZ_HoaDonNhap HDN = BIZ_HoaDonNhap.getByID(this.MaHDNhap);
                if (HDN != null)
                {
                    HDN.TongTien = BIZ_ChiTietNhap.getTongTien(this.MaHDNhap);
                    Console.WriteLine(HDN.TongTien);
                    HDN.Update();
                }
            }
            return success;
        }
        public bool Update()
        {
            bool success= DAL_ChiTietNhap.Update(this);
            if (success)
            {
                BIZ_HoaDonNhap HDN = BIZ_HoaDonNhap.getByID(this.MaHDNhap);
                if (HDN != null)
                {
                    HDN.TongTien = BIZ_ChiTietNhap.getTongTien(this.MaHDNhap);
                    Console.WriteLine(HDN.TongTien);
                    HDN.Update();
                }
            }
            return success;
        }
        public bool Delete()
        {
            bool success= DAL_ChiTietNhap.Delete(this);
            if (success)
            {
                BIZ_HoaDonNhap HDN = BIZ_HoaDonNhap.getByID(this.MaHDNhap);
                if (HDN != null)
                {
                    HDN.TongTien = BIZ_ChiTietNhap.getTongTien(this.MaHDNhap);
                    Console.WriteLine(HDN.TongTien);
                    HDN.Update();
                }
            }
            return success;
        }
        #endregion
    }
}
